package servlet;

import com.google.protobuf.TextFormat;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Test;
import pojo.wenxian1;
import service.impl.wenxianimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.net.URLEncoder;
import java.sql.Date;
import java.util.Collection;
import java.util.List;
import java.util.UUID;


@WebServlet(name = "UploadServlet",urlPatterns = "/UploadServlet")
@MultipartConfig
public class UploadSevlet extends HttpServlet {
    private service.impl.wenxianSerice wenxianSerice=new wenxianimpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String name1 = request.getParameter("name");
        String upload_date = request.getParameter("upload_date");
        System.out.println(name1);
        String document_type = request.getParameter("document_type");
        System.out.println(document_type);
        String keywords = request.getParameter("keywords");
        Date rq;
        String wenjia=null;
        rq = Date.valueOf(upload_date);
        System.out.println(rq);
        // 创建保存上传文件的文件夹路径
        String savePath = "D:\\wenxian-master\\wenxian-master\\src\\main\\webapp\\upload\\";

        // 创建文件夹路径（如果不存在）
        File fileSaveDir = new File(savePath);
        if (!fileSaveDir.exists()) {
            fileSaveDir.mkdirs();
        }

        // 从请求中获取所有的表单字段和文件部分
        Collection<Part> parts = request.getParts();
        for (Part part : parts) {
            String filename = extractFileName(part);
            if (filename != null && !filename.equals("")) {
                // 保存文件到指定路径
                PrintWriter writer=response.getWriter();
                writer.print("<html>\n" +
                        "<head>\n" +
                        "    <meta charset=\"UTF-8\">\n" +
                        "    <link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\n" +
                        "    <script src=\"script.js\"></script>\n" +
                        "    <style>\n" +
                        "        .modal {\n" +
                        "            display: none;\n" +
                        "            position: fixed;\n" +
                        "            top: 0;\n" +
                        "            left: 0;\n" +
                        "            width: 100%;\n" +
                        "            height: 100%;\n" +
                        "            background-color: rgba(0, 0, 0, 0.5);\n" +
                        "        }\n" +
                        "\n" +
                        "        .modal-content {\n" +
                        "            background-color: #fff;\n" +
                        "            width: 300px;\n" +
                        "            margin: 200px auto;\n" +
                        "            padding: 20px;\n" +
                        "            text-align: center;\n" +
                        "        }\n" +
                        "\n" +
                        "        h1 {\n" +
                        "            color: #333;\n" +
                        "            font-size: 24px;\n" +
                        "            margin-bottom: 10px;\n" +
                        "        }\n" +
                        "\n" +
                        "        p {\n" +
                        "            color: #666;\n" +
                        "            font-size: 16px;\n" +
                        "            margin-bottom: 20px;\n" +
                        "        }\n" +
                        "\n" +
                        "        #close-btn {\n" +
                        "            background-color: #333;\n" +
                        "            color: #fff;\n" +
                        "            border: none;\n" +
                        "            padding: 10px 20px;\n" +
                        "            font-size: 16px;\n" +
                        "            cursor: pointer;\n" +
                        "        }\n" +
                        "        a{\n" +
                        "            background-color: #333;\n" +
                        "            color: #fff;\n" +
                        "            border: none;\n" +
                        "            padding: 10px 20px;\n" +
                        "            font-size: 16px;\n" +
                        "            cursor: pointer;\n" +
                        "        }\n" +
                        "    </style>\n" +
                        "</head>\n" +
                        "<body>\n" +
                        "\n" +
                        "<div id=\"success-modal\" class=\"modal\" style=\"display: block\">\n" +
                        "<!-- 上传成功弹窗 -->\n" +
                        "    <div class=\"modal-content\">\n" +
                        "        <h1>文件上传成功</h1>\n" +
                        "        <p>你的文件已成功上传。</p>\n" +
                        "        <button id=\"close-btn\">关闭</button>\n" +
                        "    </div>\n" +
                        "</div>\n" +
                        "<script>\n" +
                        "\n" +
                        "    // 获取上传表单和弹窗元素\n" +
                        "    const successModal = document.getElementById(\"success-modal\");\n" +
                        "    const closeModalBtn = document.getElementById(\"close-btn\");\n" +
                        "    closeModalBtn.innerHTML= \"\\t<a href=\\\"#\\\" class=\\\"roll-nav roll-right tabReload\\\"><i class=\\\"fa fa-refresh\\\"></i> 关闭</a>\";\n" +
                        "    // 关闭按钮的点击事件\n" +
                        "    closeModalBtn.addEventListener(\"click\", () => {\n" +
                        "        // 隐藏弹窗\n" +
                        "        successModal.style.display = \"none\";\n" +
                        "        window.open(\"http://localhost:8080/wenxian-master/up.html\",\"_self\")\n" +
                        "    });\n" +
                        "</script>\n" +
                        "</body>\n" +
                        "</html>");
                filename = UUID.randomUUID().toString() + "_" + filename;
                wenjia=filename;
                part.write(savePath + File.separator + filename);
            }
        }
        String a = "D:\\wenxian-master\\wenxian-master\\src\\main\\webapp\\upload\\" + wenjia;
        System.out.println(a);
ce ce=new ce();
        String pdf = ce.pdf(a);
        wenxian1 wenxian1 = new wenxian1();
        wenxian1.setWen(pdf);
        wenxian1.setDi(wenjia);
        wenxian1.setGuan(keywords);
        wenxian1.setLei(document_type);
        wenxian1.setRq(rq);
        wenxian1.setZuo(name1);
        wenxianSerice.into(wenxian1);
    }
    private String extractFileName(Part part) {
        String contentDisp = part.getHeader("content-disposition");
        String[] items = contentDisp.split(";");
        for (String item : items) {
            if (item.trim().startsWith("filename")) {
                return item.substring(item.indexOf("=") + 2, item.length() - 1);
            }
        }
        return null;
    }
}
